import logging


class Logger:
    """公共logger"""
    def __init__(self, **kwargs):
        self.name: str = ''
        self.handler: str = 'StreamHandler'
        self.handler_kwargs: dict = {}
        self.level: str = 'INFO'
        self.formatter = '%(name)s - %(asctime)s - %(levelname)s - %(filename)s - %(lineno)d - %(message)s'
        self.logger = None

        if kwargs:
            self.init(**kwargs)

    def init(self, name='', handler='', handler_kwargs: dict = None, level: str = None, formatter: str = ''):
        if name:
            self.name = name
        if handler:
            self.handler = handler
        if handler_kwargs:
            self.handler_kwargs = handler_kwargs
        if level:
            self.level = level
        if formatter:
            self.formatter = formatter

        self.logger = logging.getLogger(self.name)
        self.logger.setLevel(level=self.level)
        handler = getattr(logging, self.handler)(**self.handler_kwargs)
        formatter = logging.Formatter(self.formatter)
        handler.setFormatter(formatter)

        if not self.logger.handlers:
            self.logger.addHandler(handler)


logger = Logger()


if __name__ == '__main__':
    logger.init(name='aa', handler='StreamHandler', level='DEBUG')

    logger = logging.getLogger('aa')  # 外部引用
    logger.info("Start print log")
    logger.debug("Do something")
    logger.warning("Something maybe fail.")
    logger.info("Finish")
